﻿using DBTest.Table;
using HB.CacheDb;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace DBTest
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {


            Log("begin init db data");
            using (var conn = GetConn())
            {
                conn.CreateTable<Student>();
                conn.CreateTable<Teacher>();
                if (conn.Table<Student>().Count() < 1)//first time insert data
                {
                    Random Ran = new Random();
                    List<Student> ls = new List<Student>();
                    for (int i = 0; i < 100; i++)
                    {
                        ls.Add(new Student()
                        {
                            age = 15 + Ran.Next(1, 5),
                            classnum = Ran.Next(1, 16),
                            name = $"学生{i}",
                            sex = (Ran.Next(1, 10) % 2) == 0 ? "男" : "女"
                        });
                    }
                    conn.InsertAll(ls);
                    List<Teacher> lst = new List<Teacher>();
                    for (int i = 0; i < 100; i++)
                    {
                        lst.Add(new Teacher()
                        {
                            age = 15 + Ran.Next(1, 5),
                            classnum = Ran.Next(1, 16),
                            name = $"学生{i}",

                        });
                    }
                    conn.InsertAll(lst);
                    Log("init over");
                }

            }

            using (var conn = GetConn())
            {
                var students = conn.Table<Student>().ToList();
                var teachers = conn.Table<Teacher>().ToList();
                cache.InitializeTable<Student>(students, zz => zz.name, false, true);
                cache.InitializeTable<Teacher>(teachers, zz => zz.name, false, true);
            }
            var data = cache.Table<Student>().Where(zz => zz.name.Contains("3")).ToList();
            Log(data.Count().ToString());
            var data2 = cache.Table<Teacher>().Where(zz => zz.name.Contains("3")).ToList();
            Log(data2.Count().ToString());

            cache.Table<Student>().Add(new Student() { age = 34, classnum = 4, name = "haibao", sex = "男" });

            var data3 = cache.Table<Teacher>().Where(zz => zz.age > 15).ToList();
            data3.ForEach(d =>
            {
                Log(d.name);
            });
            Console.ReadKey();

        }

        static void Log(string msg)
        {
            Console.WriteLine(msg);
        }
        static CacheDb cache = new CacheDb(new DBSolution());
        public static SQLiteConnection GetConn()
        {
            return new SQLiteConnection("test.db");
        }
    }
    /// <summary>
    /// 实际数据库缓存方案
    /// </summary>
    public class DBSolution : ICacheDbCallBack
    {
        public bool Add<T>(T value)
        {
            using (var conn = Program.GetConn())
            {
                return conn.Insert(value) > 0;
            }
        }

        public bool AddAll<T>(List<T> datas)
        {
            using (var conn = Program.GetConn())
            {
                return conn.InsertAll(datas) > 0;
            }
        }



        public bool Delete<T>(Expression<Func<T, bool>> ex) where T : class, new()
        {
            using (var conn = Program.GetConn())
            {
                return conn.Table<T>().Delete(ex) > 0;
            }
        }



        public bool Update<T>(T obj)
        {
            using (var conn = Program.GetConn())
            {
                return conn.Update(obj) > 0;
            }
        }
        public bool UpdateAll<T>(List<T> obj)
        {
            using (var conn = Program.GetConn())
            {
                return conn.UpdateAll(obj) > 0;
            }
        }


    }


}
